//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    formType: {},
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    time: {},
    winWidth: 0,
    winHeight: 600,
    // tab切换 
    currentTab: 0,
    ontips:false,
    ontips2: false,
    list1:"",
    list2:[],
    createj:"none",
    wheight:600,
    fheight:600
  },
  onLoad: function () {
    var _this = this;
    app.userInfoReadyCallback = res => {
      this.onShow();
    }
    wx.setEnableDebug({
      enableDebug: true
    })
  },
  //授权事件
  getUserInfo_zh: function (e) {
    var _this = this
    if (e.detail.userInfo) {

      app.setUserinfo();
      _this.setData({
        hasUserInfo: false
      })
    } else {
      wx.showToast({
        title: "为了您更好的体验,请先同意授权",
        icon: 'none',
        duration: 2000
      });
    }
  },
  //创建目标事件
  createGoal:function(e){
    var _this = this;
    wx.getSetting({
      success: res => {
        if (!res.authSetting['scope.userInfo']) {
          _this.setData({
            hasUserInfo: true
          })
        }else{
          wx.navigateTo({
            url: '/pages/addgoal/addgoal',
          })
        }
      }
    })
  },
  //右下角创建事件
  createj:function(e){
    wx.navigateTo({
      url: '/pages/addgoal/addgoal',
    })
  },
  //点击详情
  activeDetail:function(e){
    console.log(e.currentTarget.id);
    wx.navigateTo({
      url: '/pages/taskInfo/taskinfo?id=' + e.currentTarget.id,
    })
  },

  //滑动模块
  bindChange: function (e) {

    var that = this;
    that.setData({ currentTab: e.detail.current });

  },
  swichNav: function (e) {
    var that = this;

    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      console.log(e.target.dataset.current)
      if (e.target.dataset.current === "0"){
        that.setData({
          winHeight:this.data.wheight
        })
      }
      if (e.target.dataset.current === "1"){
        that.setData({
          winHeight: this.data.fheight
        })
      }
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  //时间戳转化成日期
  getLocalTime:function(nS) {
    return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ');
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      if (app.globalData.userInfo == null) {
        this.setData({
          ontips: true,
          ontips2:true,
          createj:false
        })
      } else {
        wx.request({
          url: `${app.globalData.apiurl}api/index/getTask`,
          method: 'GET',
          data: {
            uid: app.globalData.userInfo.uid
          },
          success: res => {
            console.log(res)
              let fitem=[];
              let witem=[];
            let fheight = 30;
            let wheight = 30;
              let item = res.data.data.task
              for(let i =0;i<item.length;i++){
                if(item[i].is_end==1){
                if (item[i].notice_time == null){
                  item[i].notice_time = "未设定时间"
                }else{
                  item[i].notice_time = this.getLocalTime(item[i].notice_time)
                }
                  if (item[i].is_import==1){
                  item[i].is_import="red"
                }
                 fitem.push(item[i])
                 fheight=fheight+220
                }
                if (item[i].is_end == 0){
                  if (item[i].is_import == 1) {
                    item[i].is_import = "red"
                  }
                  if (item[i].notice_time == null) {
                    item[i].notice_time = "未设定时间"
                  } else {
                    item[i].notice_time = this.getLocalTime(item[i].notice_time)
                  }
                  witem.push(item[i])
                  wheight=wheight+220
                }
                
              }
            if (fheight==30){
              fheight = 600;
            }
            if (wheight==30){
              wheight = 600
            }
            console.log(witem.length)
              this.setData({
                list1:witem,
                list2:fitem,
                createj:true,
                fheight: fheight,
                wheight: wheight,
                winHeight:wheight
              })
            if (witem.length == 0) {
              this.setData({
                ontips: true,
                createj: "none"
              })
            }else{
              this.setData({
                ontips: false,
                createj: ""
              })
            }
            if(fitem.length==0){
              this.setData({
                ontips2: true,
              })
            }else{
              this.setData({
                ontips2: false,
              })
            }
          }
        })

        console.log(this.data.list1)
      }


  },
})
